import * as THREE from 'three'
import { createScanEffect } from './scanEffect.js'
import { getBox3Info } from './object3DTools.js'

const points = [
  new THREE.Vector3(0, 0, 15),
  new THREE.Vector3(0, 0, -5.5),
  new THREE.Vector3(4.4, 0, -5.5),
]
const curve = new THREE.CurvePath()
// 共有length个点, , 往返各length-1个线段, 共2*length-2个线段
for (let i = 0; i < 2 * points.length - 2; i++) {
  if (i < points.length - 1) {
    //前length-1个线段, 线段起点的索引最大为length-2
    curve.add(new THREE.LineCurve3(points[i], points[i + 1]))
  } else {
    //后length-1个线段, 逆向, 第一个线段起点的索引从最后一个(length-1)开始算起
    const index = 2 * points.length - 2 - i
    curve.add(new THREE.LineCurve3(points[index], points[index - 1]))
  }
}
curve.arcLengthDivisions = 1000

const pausePoints = [
  new THREE.Vector3(0, 0, 9.5),
  new THREE.Vector3(0, 0, 2),
  new THREE.Vector3(0, 0, -3.4),
]

export default class Robot {
  /**
   *
   * @param {THREE.Object3D | THREE.Group} robot 机器人object
   * @param {THREE.Vector3} [position] 机器人初始位置, 默认值为(0,0,0)
   * @param {THREE.Vector3} [scale] 机器人缩放, 默认值为(1,1,1)
   * @param {THREE.Vector3} [lookAt] 机器人朝向, 默认值为(0,0,0)
   */
  constructor(
    robot,
    position = new THREE.Vector3(),
    scale = new THREE.Vector3(1, 1, 1),
    lookAt = new THREE.Vector3(),
  ) {
    this.robot = robot.clone()
    this.robot.position.copy(position)
    this.robot.scale.copy(scale)
    this.robot.lookAt(lookAt.x, lookAt.y, lookAt.z)
    this.addScanEffect()
    this.setPath()
    this.setAnimation()
  }
  addScanEffect(radius = 1) {
    const { size, center, scale } = getBox3Info(this.robot)
    this.scanGroup = createScanEffect({
      object3d: this.robot,
      radius: radius / scale,
      x: size.x / 2 / scale,
      y: center.y / scale,
    })
  }
  setPath(path = curve) {
    this.path = path
  }
  /**
   *
   * @param {Number} [pauseDuration] 停留时间 毫秒,默认2000
   * @param {Number} [threshold] 触发停留的距离阈值 默认0.1
   * @param {Number} [speed] 速度 默认0.005
   */
  setAnimation(pauseDuration = 2000, threshold = 0.1, speed = 0.002, stopPoints = pausePoints) {
    this.progress = 0
    //暂停移动点位
    this.isPaused = false
    this.pauseEndTime = 0
    this.pauseDuration = pauseDuration // 停留2秒
    this.threshold = threshold // 触发停留的距离阈值
    this.speed = speed
    this.lastStopPoint = null
    this.stopPoints = stopPoints
    this.isScanning = false
  }
  romaAnimation = () => {
    const { pauseDuration, speed, stopPoints, threshold, path } = this
    // 处理扫描效果
    if (this.isScanning) {
      this.scanGroup.rotateY(0.05)
    } else {
      this.scanGroup.rotation.y = 0
    }

    //处理暂停状态
    if (this.isPaused) {
      if (Date.now() >= this.pauseEndTime) {
        this.isPaused = false // 取消暂停, 恢复动画
        this.toggleScanEffect(false)
      } else {
        return // 暂停中，中止函数 ,不更新位置
      }
    }

    this.progress = this.progress % 1
    const pos = path.getPointAt(this.progress)

    // 检测位置是否接近暂停点
    // 确保暂停位置不会频繁触发
    if (!this.lastStopPoint || pos.distanceTo(this.lastStopPoint) >= threshold) {
      for (const point of stopPoints) {
        if (this.lastStopPoint === point) continue
        if (pos.distanceTo(point) < threshold) {
          this.isPaused = true
          this.pauseEndTime = Date.now() + pauseDuration
          this.lastStopPoint = point
          this.toggleScanEffect(true)
          return
        }
      }
    }

    this.progress = (this.progress + speed) % 1
    const nextPos = path.getPointAt(this.progress)
    this.robot.position.set(pos.x, pos.y, pos.z)
    this.robot.lookAt(nextPos.x, nextPos.y, nextPos.z)
  }
  toggleScanEffect(show) {
    this.isScanning = show
    this.scanGroup.visible = show
  }
}
